/**
  ******************************************************************************
  * @file    AT25DF041.h
  * @author  Thongnh
  * @version V1.0
  * @date    18-07-2011
  * @brief   Main process of Flash AT25DF041
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, XICUTELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Xicutelectronics</center></h2>
  ******************************************************************************
  */ 

#ifndef __USER_H__
#define __USER_H__

#include "BasicTypes.h"


class cUser
{
private:
    static const U32 PARA_BLOCK_ADDR = 0x00000000;
    static const U32 USER_BLOCK_ADDR = 0x00010000;
    static const U32 USER_BLOCK_SIZE = 0x00010000;

    static const U16 USER_MAX_CNT = 8192;
    static const U16 USER_READ_MARK_MAX_CNT = USER_MAX_CNT / 32 + 1;
    static const U8  USER_LENGTH = 8;

    static const U8  VALID_USER = 0xA5;
    static const U8  ERASE_USER = 0x5A;
    static const U8  INVALID_USER = (ERASE_USER & VALID_USER);

private:
    static U16 s_TotalUser;
    static U32 s_UserReadMark[];

private:
    static bool GetPara();
    static bool Lookup(U8 *pCard, U32 &iAddr, U16 &userID);
    static bool FindFree(U32 &iAddr);
    static void IncTotalUser();
    static void DecTotalUser(bool bEraseAll);

public:
    static void InitReset(void);
    static bool FindUser(U32 iCardCode, U16 &timeZone, U16 &userID);
    static bool Register(U32 iCardCode, U16 iTimeZone);
    static bool Register(U8 *pCard);
    static bool Erase(U8 *pCard);
    static bool EraseAll();

    static void ResetUserReadMark();
    static void SetUserRead(U16 iUserID);
    static bool IsUserRead(U16 iUserID);

    static inline U16 GetTotalUser()
    {
        return (s_TotalUser);
    }
};

#endif
// end of file

